package shuzi.adapter;

import java.util.List;

import shuzi.data.Database;
import shuzi.model.BrandPhone;
import shuzi.widget.BrandImageView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shuzi.offer.R;

public class OrderAdapter extends BaseAdapter {

	private final LayoutInflater mInflater;
	private List<BrandPhone> list;
	private Context context;

	public OrderAdapter(Context context, List<BrandPhone> list) {
		this.mInflater = LayoutInflater.from(context);
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public final class ViewHolder {
		public BrandImageView img;
		public TextView name;
		public TextView price;
		public TextView number;
		public TextView color;
		public Button jia;
		public Button jian;

		private final View baseView;

		public ViewHolder(View baseView) {
			this.baseView = baseView;
		}

		public TextView getName() {
			return (TextView) baseView.findViewById(R.id.orderName);
		}

		public void setName(TextView name) {
			this.name = name;
		}

		public TextView getPrice() {
			return (TextView) baseView.findViewById(R.id.orderPrice);
		}

		public void setPrice(TextView price) {
			this.price = price;
		}

		public TextView getNumber() {
			return (TextView) baseView.findViewById(R.id.number);
		}

		public void setNumber(TextView number) {
			this.number = number;
		}

		public TextView getColor() {
			return (TextView) baseView.findViewById(R.id.color);
		}

		public void setColor(TextView color) {
			this.color = color;
		}

		public Button getJia() {
			return (Button) baseView.findViewById(R.id.plus);
		}

		public void setJia(Button jia) {
			this.jia = jia;
		}

		public Button getJian() {
			return (Button) baseView.findViewById(R.id.jian);
		}

		public void setJian(Button jian) {
			this.jian = jian;
		}

		public BrandImageView getImg() {
			return (BrandImageView) baseView.findViewById(R.id.orderImage);
		}

		public void setImg(BrandImageView img) {
			this.img = img;
		}

	}
	
	/**
	 * 加号监听器
	 * @author Administrator
	 *
	 */
	public class JiaOnClickLister implements OnClickListener {

		int position;
		Button jia;
		TextView number;
		TextView zongjia;

		public JiaOnClickLister(int position, Button jia,TextView number,TextView zongjia) {
			this.position = position;
			this.jia = jia;
			this.number = number;
			this.zongjia = zongjia;
		}

		@Override
		public void onClick(View v) {
			int count = list.get(position).getNumber()+1;
		     number.setText(String.valueOf(count));
		     list.get(position).setNumber(count);
		     int zong = Integer.parseInt(list.get(position).getPrice())*count;
		     //System.out.println("zong = " +zong);
		     zongjia.setText("￥"+String.valueOf(zong));
		     //Database.uploadList.get(position).setTotalPrice(zong+"");
		     setZongjia();
		}
	}
	
	
	/**
	 *减号监听器
	 * @author Administrator
	 *
	 */
	public class JianOnClickLister implements OnClickListener {

		int position;
		Button jian;
		TextView number;
		TextView zongjia;

		public JianOnClickLister(int position, Button jian,TextView number,TextView zongjia) {
			this.position = position;
			this.jian = jian;
			this.number = number;
			this.zongjia = zongjia;
		}

		@Override
		public void onClick(View v) {
			if(list.get(position).getNumber()==1){
				return;
			}
			int count =list.get(position).getNumber()-1;
		     number.setText(String.valueOf(count));
		     list.get(position).setNumber(count);
		     int zong = Integer.parseInt(list.get(position).getPrice())*count;
		     zongjia.setText("￥"+String.valueOf(zong));
		     setZongjia();
		}
	}
	
	/**
	 * 外部重写方法
	 */
	public void setZongjia(){
		
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = (RelativeLayout) this.mInflater.inflate(
					R.layout.order_item, null);
			holder = new ViewHolder(convertView);
			holder.img = holder.getImg();
			holder.color = holder.getColor();
			holder.jia = holder.getJia();
			holder.jian = holder.getJian();
			holder.name = holder.getName();
			holder.price = holder.getPrice();
			holder.number = holder.getNumber();

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(list.get(position).getNumber()!=1){
			holder.number.setText(list.get(position).getNumber()+"");
		}else{
			holder.number.setText(1+"");
		}
		holder.name.setText(list.get(position).getName());
		holder.price.setText("￥"+Integer.parseInt(list.get(position).getPrice())
				* list.get(position).getNumber() + "");
		holder.jia.setOnClickListener(new JiaOnClickLister(position, holder.jia, holder.number, holder.price));
		holder.jian.setOnClickListener(new JianOnClickLister(position, holder.jian, holder.number, holder.price));
		holder.img.setImageViewBg(list.get(position).getPicture(),
				list.get(position).getName(),position,Database.PHONE_INFO);
		
		
		switch (list.get(position).getChoiceColor()) {
		case Database.WHITE:
			holder.color.setText("白色");
			break;

		case Database.BLACK:
			holder.color.setText("黑色");
			break;
		case Database.OTHER:
			holder.color.setText("其他");
			break;

		default:
			holder.color.setText("白色");
			break;
		}

		return convertView;
	}

}
